package com.feng.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feng.reggie.common.R;
import com.feng.reggie.dto.SetmealDto;
import com.feng.reggie.entity.Category;
import com.feng.reggie.entity.Dish;
import com.feng.reggie.entity.Setmeal;
import com.feng.reggie.entity.SetmealDish;
import com.feng.reggie.service.CategoryService;
import com.feng.reggie.service.SetmealDishService;
import com.feng.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("套餐信息：{}",setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功！");


    }

    /**
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //分页构造器
        Page<Setmeal> pageinfo=new Page<>(page,pageSize);
        Page<SetmealDto> dtoPage=new Page<>();

        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        //添加查询条件，根据name进行like的模糊查询
        queryWrapper.like(name!=null,Setmeal::getName,name);
        //添加一个排序条件
        queryWrapper.orderByAsc(Setmeal::getUpdateTime);
        setmealService.page(pageinfo,queryWrapper);
        //对象的拷贝
        BeanUtils.copyProperties(pageinfo,dtoPage,"record");
        List<Setmeal> records = pageinfo.getRecords();
        List<SetmealDto> list=records.stream().map((item)->{
            SetmealDto setmealDto=new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            Long categoryId=item.getCategoryId();
            Category byId = categoryService.getById(categoryId);
            if(byId!=null){
                setmealDto.setCategoryName(byId.getName());
            }

            return setmealDto;
        }).collect(Collectors.toList());


//        BeanUtils.copyProperties(pageinfo,dtoPage,"record");
//        List<Setmeal> records = pageinfo.getRecords();
//        List<SetmealDto> list= records.stream().map((item)->{
//            SetmealDto setmealDto=new SetmealDto();
//            BeanUtils.copyProperties(item,setmealDto);
//            Long catagoryid=item.getCategoryId();
//            Category category = categoryService.getById(catagoryid);
//            setmealDto.setCategoryName(category.getName());
//            return setmealDto;
//
//        }).collect(Collectors.toList());
//         dtoPage.setRecords(list);
        dtoPage.setRecords(list);

        return R.success(dtoPage);
    }

    /**
     * 套餐删除
     * @param ids
     * @return
     */
//    @DeleteMapping
//    public R<String> delete(String[] ids){
//        for (String id : ids) {
//            Setmeal byId = setmealService.getById(id);
//            //判断套餐销售状态
//            if(byId.getStatus()==1){
//                return R.error("该套餐正在售卖，不能进行删除！");
//            }
//            setmealService.removeById(id);
//        }
//        return R.success("套餐删除成功！");
//    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @Transactional
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("ids:{}",ids);
        setmealService.removeWithDish(ids);
        return R.success("套餐数据删除成功！");
    }

    /**
     * 修改套餐销售状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> sale(@PathVariable int status,String[] ids) {
        for (String id : ids) {
            Setmeal byId = setmealService.getById(id);
            byId.setStatus(status);
            setmealService.updateById(byId);
        }
        return R.success("状态修改成功！");

    }
    /**
     * 回显套餐数据，根据套餐id回显数据
     */

    @GetMapping("/{id}")
    public R<SetmealDto> getData(@PathVariable Long id){

        SetmealDto date = setmealService.getDate(id);
       return R.success(date);



    }

    /**
     * 保存修改后的数据，需要操作套餐表和套餐菜品关系表
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String> save1(@RequestBody SetmealDto setmealDto){
        if(setmealDto==null){
            return R.error("请求异常");
        }
        if(setmealDto.getSetmealDishes()==null){
            return R.error("套餐中没有菜品，请重新添加！");
        }
        //先更新套餐表
        setmealService.updateById(setmealDto);
        //再保存套餐菜品关系表
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        //先将菜品关联表中的数据删除
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);
        //再为套餐关系表打上相应的套餐id
        setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        //最后保存菜品信息表
        setmealDishService.saveBatch(setmealDishes);

      return R.success("套餐修改成功！");
    }

    /**
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
    LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
    queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
    queryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
    queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }


}
